import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/ui/ui.dart';
import 'package:get/get.dart';

class UiActionSheetItemData {
  String title;
  IconData? icon;
  Color? color;
  bool disabled;
  dynamic value;
  UiActionSheetItemData({
    this.title = '',
    this.value,
    this.icon,
    this.color,
    this.disabled = false,
  });
}

class UiActionSheet extends StatelessWidget {
  const UiActionSheet({
    super.key,
    this.title,
    this.titleWidget,
    this.subtitle,
    this.onTap,
    this.items = const [],
    this.alignment = MainAxisAlignment.center,
    this.titleFontSize = 14,
    this.cancelText,
  });
  final String? title;
  final String? subtitle;
  final Widget? titleWidget;
  final List<UiActionSheetItemData> items;
  final MainAxisAlignment alignment;
  final Function(dynamic value)? onTap;
  final double titleFontSize;
  final String? cancelText;
  static show({
    String? title,
    String? subtitle,
    Widget? titleWidget,
    List<UiActionSheetItemData> items = const [],
    MainAxisAlignment alignment = MainAxisAlignment.center,
    Function(dynamic value)? onTap,
    double titleFontSize = 14,
    String? cancelText,
  }) async {
    return Ui.showBottomSheet(UiActionSheet(
      title: title,
      subtitle: subtitle,
      titleWidget: titleWidget,
      items: items,
      alignment: alignment,
      onTap: onTap,
      titleFontSize: titleFontSize,
      cancelText: cancelText,
    ));
  }

  closeSheet(context, type) {
    if (Navigator.of(context).canPop()) {
      Navigator.of(context).pop(type);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      clipBehavior: Clip.hardEdge,
      decoration: BoxDecoration(
        color: UiTheme(context).matchColor(
          Colors.white,
          UiTheme(context).listCellBackgroundColor,
        ),
        borderRadius: const BorderRadius.vertical(
          top: Radius.circular(UiTheme.boxRadiusX),
        ),
      ),
      child: DefaultTextStyle(
        style: TextStyle(
            fontSize: 16,
            color: UiTheme(context)
                .match(light: Colors.black, dark: Colors.white)
                .withOpacity(0.8)),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (title != null)
              Padding(
                padding: const EdgeInsets.all(15),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '$title',
                      textAlign: TextAlign.center,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(fontSize: titleFontSize),
                    ),
                    if (subtitle != null)
                      Padding(
                        padding: const EdgeInsets.only(top: 5.0),
                        child: Text(
                          '$subtitle',
                          textScaler: const TextScaler.linear(0.8),
                          style: TextStyle(
                            fontSize: titleFontSize,
                            color: Colors.black54,
                          ),
                        ),
                      ),
                  ],
                ),
              ),
            Container(
              child: titleWidget,
            ),
            const Divider(
              height: 0,
              thickness: 0.1,
            ),
            for (var i = 0; i < items.length; i++)
              UiListCell(
                dividerIndent: 0,
                title: Row(mainAxisAlignment: alignment, children: [
                  if (items[i].icon != null)
                    Padding(
                      padding: const EdgeInsets.only(right: 8.0),
                      child: Icon(
                        items[i].icon,
                        size: 20,
                        color: items[i].color,
                      ),
                    ),
                  Text(
                    items[i].title,
                    style: TextStyle(
                        color:
                            items[i].disabled ? Colors.grey : items[i].color),
                  )
                ]),
                showDivider: items.length - 1 > i,
                showArrow: false,
                onTap: items[i].disabled
                    ? null
                    : () async {
                        closeSheet(context, items[i]);
                        onTap?.call(items[i].value ?? items.length - 1 - i);
                      },
              ),
            Divider(
              height: 8,
              thickness: 8,
              color: UiTheme(context)
                  .matchColor(Colors.grey[100], UiTheme.deepBlackBlue),
            ),
            UiListCell(
              hoverColor: const Color.fromARGB(255, 200, 200, 200),
              padding: EdgeInsets.only(
                  left: 15,
                  right: 15,
                  top: 15,
                  bottom: max(15, MediaQuery.of(context).viewPadding.bottom)),
              onTap: () {
                closeSheet(context, 'canceled');
              },
              title: Center(
                child: Text(
                  cancelText ?? 'Cancel'.tr,
                  style: TextStyle(
                      color: UiTheme(context).normalColor.withOpacity(0.5)),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
